#include "TaskBarIcon.h"
#include "../StdUserInterfaceModule/Icons.h"
#include "../../core/Impp.h"

namespace Impp
{

BEGIN_EVENT_TABLE(TaskBarIcon, wxTaskBarIcon)
	EVT_TASKBAR_LEFT_UP(TaskBarIcon::onLeftUp)
	EVT_TIMER(ID_TIMER, TaskBarIcon::onBlink)
END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS(StdTaskBarIconModule, iModule)

TaskBarIcon::TaskBarIcon() :
	wxTaskBarIcon(),unreadMsgs(false),blinkSwitch(false),timer(this,ID_TIMER)
{
	iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
	if(ui != NULL){
		wxBitmap iconBitmap = ui->getIcons()->getSpecialBitmap(BITMAP_IMPP_ICON);
		wxBitmap iconNewMsgBitmap = ui->getIcons()->getSpecialBitmap(BITMAP_NEW_MSG);
		if(!iconBitmap.IsNull() && !iconNewMsgBitmap.IsNull()){
			iconImpp.CopyFromBitmap(iconBitmap);
			SetIcon(iconImpp, wxT("Impp"));
			iconNewMsg.CopyFromBitmap(iconNewMsgBitmap);
		}else{
			wxLogError(wxT("TaskBarIcon: No Icons defined!"));
		}
	}else{
		wxLogError(wxT("TaskBarIcon: No Userinterface found!"));
	}

	Impp* impp = Impp::exemplar();
    impp->Connect(EVT_MESSAGE_IN,MessageEventHandler(TaskBarIcon::onMessageIn), NULL, this);
    impp->Connect(EVT_MESSAGE_READ,MessageReadEventHandler(TaskBarIcon::onMessageRead), NULL, this);

}

void TaskBarIcon::onLeftUp(wxTaskBarIconEvent & event){
	Impp* impp = Impp::exemplar();

	Buddy* b = impp->getNextBuddyWithUnreadMsgs();
	if(b!=NULL){
		iChatWindow* chatWin = impp->getModuleManager()->getChatWindowModule();
		if(chatWin != NULL){
			chatWin->showChatWindow(b);
		}else{
			wxLogDebug(wxT("No Chatwindow that can be showen!"));
		}
	}else{
		iBuddyList* buddyList = impp->getModuleManager()->getBuddyListModule();
		if(buddyList){
			wxWindow* bl = buddyList->getWindow();
			bool shown = bl->IsShown();
			bl->Show(!shown);
			if(!shown){
				bl->Raise();
			}
		}else{
			wxLogDebug(wxT("No Buddylist that can be showen or hidden!"));
		}
	}
	event.Skip(true);
}

TaskBarIcon::~TaskBarIcon()
{
	Impp* impp = Impp::exemplar();
    impp->Disconnect(EVT_MESSAGE_IN,MessageEventHandler(TaskBarIcon::onMessageIn), NULL, this);
    impp->Disconnect(EVT_MESSAGE_READ,MessageReadEventHandler(TaskBarIcon::onMessageRead), NULL, this);
}

wxMenu* TaskBarIcon::CreatePopupMenu(){
	iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
	if(ui){
		wxMenu* menu = new wxMenu();
		ui->buildMainMenu(menu);
		return menu;
	}else{
		wxLogDebug(wxT("No UserInterface found to show Mainmenu!"));
	}
	return NULL;
}

void TaskBarIcon::setUnreadMsgs(bool unreadMsgs){
	wxIcon icon;
	this->unreadMsgs=unreadMsgs;
	if(unreadMsgs){
		SetIcon(iconNewMsg, wxT("Impp"));
		blinkSwitch=true;
		timer.Start(1000,false);
	}else{
		SetIcon(iconImpp, wxT("Impp"));
		timer.Stop();
	}
}

void TaskBarIcon::onBlink(wxTimerEvent & event){
	if(unreadMsgs){
		blinkSwitch=!blinkSwitch;
		wxIcon icon;
		if(blinkSwitch){
			SetIcon(iconNewMsg, wxT("Impp"));
		}else{
			SetIcon(iconImpp, wxT("Impp"));
		}

	}
}

void TaskBarIcon::onMessageIn(MessageEvent & event){
	event.Skip();
	setUnreadMsgs(true);
}

void TaskBarIcon::onMessageRead(MessageReadEvent & event){
	event.Skip();
	Buddy* buddy = Impp::exemplar()->getNextBuddyWithUnreadMsgs();
	if(buddy == NULL){
		setUnreadMsgs(false);
	}
}



}
